package kr.hkit.coffeemgn;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import kr.hkit.coffeemgn.DAO.CoffeeDAO;
import kr.hkit.coffeemgn.DTO.CoffeeDTO;
import kr.hkit.coffeemgn.ListUI.CoffeeListUI;
import kr.hkit.framework.dao.CloseDaoUtil;
import kr.hkit.framework.dao.LoadPropertyDAO;
import kr.hkit.framework.inputPanel.InputTxtPanel;
import kr.hkit.framework.inputPanel.ValidCheck;

public class CoffeeMgn extends JFrame implements ActionListener {

	private JPanel contentPane;
	private InputTxtPanel pCode;
	private InputTxtPanel pName;
	private InputTxtPanel pCost;
	private InputTxtPanel pCnt;
	private InputTxtPanel pRate;
	private JButton inputBtn;
	private Connection con;
	private CoffeeDAO dao;
	private JButton outputBtn01;
	
	private CoffeeListUI output1;
	private CoffeeListUI output2;
	private JButton outputBtn02;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					final Connection con = LoadPropertyDAO.getConnection();
					CoffeeMgn frame = new CoffeeMgn(con);
					frame.addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e) {
							super.windowClosing(e);
							CloseDaoUtil.close(con);
						}
					});
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public CoffeeMgn(Connection con) {
		this.con = con;
		dao = new CoffeeDAO();
		dao.setCon(con);
		initialize();
	}
	

	private void initialize() {
		setTitle("상품별 판매실적");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 220);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel mainPanel = new JPanel();
		contentPane.add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new GridLayout(0, 2, 0, 0));
		
		pCode = new InputTxtPanel("제품코드");
		mainPanel.add(pCode);
		
		pName = new InputTxtPanel("제품명");
		pName.getTfValue().addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				String code = pCode.getTfValue().getText().trim().toUpperCase();
				try {
					String name = dao.selectNameByCode(code);
					pCode.getTfValue().setText(code);
					pName.getTfValue().setText(name);
					pCost.getTfValue().requestFocus();
				} catch (Exception e1) {
					pCode.getTfValue().requestFocus();
					pCode.getTfValue().selectAll();
					JOptionPane.showMessageDialog(null, e1.getMessage());
				}
			}
		});
		mainPanel.add(pName);
		
		pCost = new InputTxtPanel("제품단가");
		mainPanel.add(pCost);
		
		JPanel panel_blank01 = new JPanel();
		mainPanel.add(panel_blank01);
		
		pCnt = new InputTxtPanel("판매수량");
		mainPanel.add(pCnt);
		
		JPanel panel_blank02 = new JPanel();
		mainPanel.add(panel_blank02);
		
		pRate = new InputTxtPanel("마진율");
		mainPanel.add(pRate);
		
		JPanel btnPanel = new JPanel();
		contentPane.add(btnPanel, BorderLayout.SOUTH);
		
		inputBtn = new JButton("입력");
		inputBtn.addActionListener(this);
		btnPanel.add(inputBtn);
		
		outputBtn01 = new JButton("출력1");
		outputBtn01.addActionListener(this);
		btnPanel.add(outputBtn01);
		
		outputBtn02 = new JButton("출력2");
		outputBtn02.addActionListener(this);
		btnPanel.add(outputBtn02);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == outputBtn02) {
			actionPerformedOutputBtn02(e);
		}
		if (e.getSource() == outputBtn01) {
			actionPerformedOutputBtn01(e);
		}
		if (e.getSource() == inputBtn) {
			actionPerformedInputBtn(e);
		}
	}
	protected void actionPerformedInputBtn(ActionEvent e) {
		try {
			validCheck();
			CoffeeDTO dto = getData();
			dao.insertProduct(dto);
			JOptionPane.showMessageDialog(null, "입력 완료\n");
			if (output1 != null){
				ArrayList<CoffeeDTO>list = dao.selectAllCoffeeListOrderBySaleCost();
				output1.refreshData(list);
			}
			if (output2 != null){
				ArrayList<CoffeeDTO>list = dao.selectAllCoffeeListOrderByRateCost();
				output2.refreshData(list);
			}
			clearTf();
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
		} 
		
	}

	private void clearTf() {
		pCode.getTfValue().setText("");
		pName.getTfValue().setText("");
		pCost.getTfValue().setText("");
		pCnt.getTfValue().setText("");
		pRate.getTfValue().setText("");
	}

	private CoffeeDTO getData() {
		String code = pCode.getTfValue().getText().trim();
		String name = pName.getTfValue().getText().trim();
		int cost = Integer.parseInt(pCost.getTfValue().getText().trim());
		int cnt = Integer.parseInt(pCnt.getTfValue().getText().trim());
		int rate = Integer.parseInt(pRate.getTfValue().getText().trim());
		return new CoffeeDTO(code, name, cost, cnt, rate);
	}

	private void validCheck() throws Exception {
		ValidCheck.isValidValue(pCode);
		ValidCheck.isValidValue(pName);
		ValidCheck.isValidValue(pCost, 8);
		ValidCheck.isValidValue(pCnt, 8);
		ValidCheck.isValidValue(pRate, 2);
	}


	protected void actionPerformedOutputBtn01(ActionEvent e) {
		try {
			ArrayList<CoffeeDTO>list = dao.selectAllCoffeeListOrderBySaleCost();
			output1 = new CoffeeListUI("판 매 금 액 순 위", list);
			output1.setVisible(true);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
	protected void actionPerformedOutputBtn02(ActionEvent e) {
		try {
			ArrayList<CoffeeDTO>list = dao.selectAllCoffeeListOrderByRateCost();
			output2 = new CoffeeListUI("마 진 액 순 위", list);
			output2.setVisible(true);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
}
